module.exports = class extends think.Model {
  get relation() {
    return {
      role: {
        type: think.Model.BELONG_TO,
        model: "admin_role",
        key: "role_id",
        fKey: "id"
      }
    };
  }
  /**
   *
   * @param name
   * @param field
   * @param pageNum
   * @param pageSize
   * @returns {Promise<[]>}
   */
  async list({ name, field = "*", pageNum = 1, pageSize = 10 }) {
    const where = {};
    if (name) {
      where.name = ["like", `%${name}%`];
    }

    const list = await this.field(field)
      .page(pageNum, pageSize)
      .where(where)
      .countSelect();
    return list;
  }

  async del(id) {
    const where = {};
    if (typeof id === "string") {
      where.id = id;
    } else {
      where.id = ["in", id.join(",")];
    }
    await this.where(where).delete();
  }
};
